Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

184
Views
Is there a way I can make the input to act like "display: inline-block"?

What I am trying to do is implement a simple inline editing, I'm using a normal div to display the text when the edit state is false, and render an input when the edit state is true.

The problem I'm facing is that the input doesn't wrap around the text content it's got, like a div with display: inline-block would. I tried setting the size attribute of the input to be that of the current value's length, but it's not wrapping around the content perfectly.

Any ideas how to do it? I guess contentEditable on the span itself, without an input, is an option, but I want to be able to select all the content in the input when it gets the focus.

Here is the code.

import { useState, useRef, useEffect } from "react";

export default function App() {
  const [value, setValue] = useState("test");
  const [active, setActive] = useState(false);
  const ref = useRef();

  useEffect(() => {
    ref.current?.select();
  }, [active]);

  const length = value.split(" ").join("").length;

  return (
    <>
      {!active && <span onClick={() => setActive(true)}>{value}</span>}
      {active && (
        <input
          value={value}
          size={length}
          ref={ref}
          onChange={(e) => setValue(e.target.value)}
          onBlur={() => setActive(false)}
        />
      )}
    </>
  );
}

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!